package com.gxa.community.admin.controller.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.community.admin.advice.Log;
import com.gxa.community.admin.service.system.RoleService;
import com.gxa.community.core.pojo.domain.AdminRole;
import com.gxa.community.core.pojo.dto.system.RoleMenuDTO;
import com.gxa.community.core.pojo.dto.system.RoleNameDTO;
import com.gxa.community.core.pojo.dto.system.RolesDTO;
import com.gxa.community.core.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/role")
@Api("角色管理")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @ApiOperation("查询所有的角色")
    @GetMapping("/getallrole")
    public R getAllRole(@RequestParam("currentPage") Integer currentPage,@RequestParam("pageSize") Integer pageSize){
        //设置分页条件
        PageHelper.startPage(currentPage,pageSize);
        List<AdminRole> list = roleService.list();
        if (list != null) {
            PageInfo<AdminRole> pageInfo = new PageInfo<>(list);
            RolesDTO rolesDTO = new RolesDTO();
            rolesDTO.setTotal(pageInfo.getTotal());
            rolesDTO.setPages(pageInfo.getPages());
            rolesDTO.setCurrentPage(currentPage);
            rolesDTO.setPageSize(pageSize);
            rolesDTO.setRoles(pageInfo.getList());
            return R.ok().put("data",rolesDTO);
         }else {
            return R.error(600,"查询失败，请联系管理员");
        }
    }

    @ApiOperation("获取所有角色名称")
    @GetMapping("/getallrolename")
    public R getAllRoleName(){
        List<RoleNameDTO> allRoleNames = roleService.getAllRoleNames();

        return R.ok("查询成功").put("data",allRoleNames);
    }

    @ApiOperation("根据id删除单个角色")
    @DeleteMapping("/deletesinglerole/{id}")
    @Log(title = "",action = "删除了一个角色")
    public R deleteSingleRoleById(@PathVariable("id") Integer id){
        //既要删除角色表，还要删除角色菜单中间表
        boolean result1 = roleService.removeById(id);
        Boolean result2 = roleService.delRoleMenuRelativeByRoleId(id);
        return result1&&result2?R.ok("删除成功！"):R.error(600,"删除失败，请联系管理员");
    }

    @ApiOperation("根据角色id查询单个角色信息")
    @GetMapping("/getsingleroleinfo/{id}")
    public R getSingleRoleInfo(@PathVariable("id") Integer id){
        return roleService.getSingleRoleInfo(id);
    }

    @ApiOperation("查询所有权限")
    @GetMapping("/getallmenu")
    public R getAllMenu(){
        return roleService.getAllMenu();
    }

    @ApiOperation("新增角色")
    @PostMapping("/addrole")
    @Log(title = "",action = "新增了一个角色")
    public R addRole(@RequestBody RoleMenuDTO roleMenuDTO){
        log.info("前端传入的数据：{}",roleMenuDTO);
        return roleService.addRole(roleMenuDTO);
    }

    @ApiOperation("根据角色id修改角色信息")
    @PutMapping("/updatesinglerole")
    @Log(title = "",action = "修改了角色信息")
    public R updateSingleRole(@RequestBody RoleMenuDTO roleMenuDTO){
        return roleService.updataSingleRole(roleMenuDTO);
    }
}
